package com.youhome.service.service.impl;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.ResultCode;
import com.youhome.service.mapper.HouseAppointmentMapper;
import com.youhome.service.model.HouseAppointment;
import com.youhome.service.parameter.ParamAppointmentHouse;
import com.youhome.service.service.HouseAppointmentService;
import com.youhome.service.service.JSMSValidateService;
import com.youhome.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by chen365 on 2018/06/28.
 */
@Service
public class HouseAppointmentServiceImpl implements HouseAppointmentService {
    @Autowired
    private HouseAppointmentMapper houseAppointmentMapper;
    @Autowired
    private JSMSValidateService jsmsValidateService;

    @Override
    public int insert(HouseAppointment record) {
        return houseAppointmentMapper.insertSelective(record);
    }

    @Override
    public void appointmentHouse(ParamAppointmentHouse record) throws CustomException {

        HouseAppointment entity = new HouseAppointment();
        entity.setName(record.getName());
        entity.setPhone(record.getPhone());
        entity.setAppointmentTime(DateUtils.StrToDate(record.getAppointmentTime(), "yyyy-MM-dd"));
        entity.setType(record.getType());

        jsmsValidateService.validate(record.getPhone(), record.getMsgId(), record.getMsg());
        if (houseAppointmentMapper.insert(entity) < 1) {
            throw new CustomException(ResultCode.FAIL);
        }
    }
}
